//
//  SYJWelcomeViewController.swift
//  weibo
//
//  Created by 孙英建 on 16/4/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class SYJWelcomeViewController: UIViewController {

    override func loadView() {
        view = backgroundImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        setIconViewAnimation()
    }
    
    // 设置动画
    private func setIconViewAnimation(){
        iconView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        
        UIView.animateWithDuration(2, delay: 0.25, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 显示label
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                     // 发通知切换视图
                      NSNotificationCenter.defaultCenter().postNotificationName("switchDefaultController", object: "welcome")
                })
        }
        
    }

    // 添加控件
    private func setUI(){
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 头像的约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
            make.size.equalTo(CGSize(width: 85, height: 85))
            
        }
        
        // 欢迎label
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
    

    // MARK: - 懒加载控件
    
    // 背景图片
    private lazy var backgroundImageView: UIImageView = {
        let bgView = UIImageView(image: UIImage(named: "ad_background"))
        return bgView
    }()
    
    // 用户头像
    private lazy var iconView: UIImageView = {
       let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius = 85 * 0.5
        icon.layer.borderColor = UIColor.orangeColor().CGColor
        icon.layer.borderWidth = 1
        icon.layer.masksToBounds = true
        
        icon.sd_setImageWithURL(NSURL(string: SYJUserAccountTools.sharedTools.userAccount?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        
        return icon
    }()
    
    // 欢迎回来
    private lazy var welcomeLabel: UILabel = {
       let welLabel = UILabel()
        welLabel.textColor = UIColor.darkGrayColor()
        welLabel.font = UIFont.systemFontOfSize(17)
        welLabel.textAlignment = NSTextAlignment.Center
        welLabel.text = "欢迎回来"
        welLabel.alpha = 0
        
        return welLabel
    }()
    

}














